using System;
using System.Collections.Generic;
using UnityEngine;
using Random = UnityEngine.Random;

namespace Yoozoo.Gameplay.Liberty.StreetEvent
{
    public class StreetFightPropItemPoint : MonoBehaviour
    {
        [Header("掉落包ID")]
        public int DropId;
        [Header("刷新间隔")]
        public float FreshTime;
        [Header("最大刷新数量")] 
        public int MaxFreshNum;

        
        private float _currentTime = 0;
        private int _currentNum;
        private List<GTA_game_dropgroup> _dropGroupsConfigs;
        private int totalWeight = 0;
        [HideInInspector]
        public Transform[] pointList;
        [HideInInspector]
        public Dictionary<int, PropItem> createdMap;
        [HideInInspector]
        public List<long> addedItemList = new List<long>(4);
        private void Start()
        {
            pointList = transform.GetComponentsInChildren<Transform>();
            createdMap = new Dictionary<int, PropItem>(pointList.Length);
            PropItem.OnPropItemTaken += OnPropItemTaken;
        }

        private void OnDestroy()
        {
            PropItem.OnPropItemTaken -= OnPropItemTaken;
            ClearAllItem();
        }

        private void ClearAllItem()
        {
            foreach (var pairs in createdMap)
            {
                PropItem.DestroyPropItem(pairs.Value);
            }

            _dropGroupsConfigs = null;
            createdMap.Clear();
            createdMap = null;
            addedItemList.Clear();
            addedItemList = null;
            pointList = null;
        }

        private void Update()
        {
            if (StreetFightEvent.Instance == null || !StreetFightEvent.Instance.DropItemConfigLoaded)
            {
                return;
            }

            if (_dropGroupsConfigs == null)
            {
                _dropGroupsConfigs = LibertyConfigManager.GetInstance().GetGTA_game_dropgroupConfigs(DropId);
                if (_dropGroupsConfigs == null)
                {
                    Debug.LogError("加载道具配置失败,dropId:" + DropId);
                    return;
                }
            }

            if (totalWeight == 0)
            {
                foreach (var config in _dropGroupsConfigs)
                {
                    totalWeight += config.weight;
                }
            }
            if (_currentTime >= FreshTime)
            {
                GenerateItem(GetGeneratePos());
                _currentTime = 0;
            }
            _currentTime += Time.deltaTime;
        }

        private int GetGeneratePos()
        {
            if (_currentNum < MaxFreshNum)
            {
                var key = Random.Range(0, pointList.Length);
                while (createdMap.ContainsKey(key))
                {
                    key = Random.Range(0, pointList.Length);
                }

                return key;
            }

            return -1;
        }

        private void GenerateItem(int index)
        {
            if (_currentNum < MaxFreshNum && index != -1)
            {
                var pointTrans = pointList[index];
                var randomWeight = Random.Range(0, totalWeight);
                var currentWeight = 0;
                foreach (var config in _dropGroupsConfigs)
                {
                    currentWeight += config.weight;
                    if (randomWeight <= currentWeight)
                    {
                        //生成道具
                        var item = PropItem.CreatePropItem(config.item_id);
                        item.transform.position = pointTrans.position;
                        addedItemList.Add(item.UID);
                        _currentNum = addedItemList.Count;
                        createdMap.Add(index,item);
                        break;
                    }
                }
            }
        }

        private void OnPropItemTaken(int id, int entityId,long UID,PropItem propItem)
        {
            addedItemList.Remove(UID);
            _currentNum = addedItemList.Count;
            var removeKey = -1;
            foreach (var pair in createdMap)
            {
                if (pair.Value.UID == UID)
                {
                    removeKey = pair.Key;
                }
            }

            if (removeKey!=-1)
            {
                createdMap.Remove(removeKey);
            }
        }
        
        public Transform GetAItemById(int itemId)
        {
            if (createdMap != null)
            {
                foreach (var pair in createdMap)
                {
                    if (pair.Value.ItemId == itemId)
                    {
                        return pair.Value.transform;
                    }
                }
            }
            return null;
        }

        public void GetAllItem(List<PropItem> list)
        {
            if (list != null && createdMap != null)
            {
                foreach (var pair in createdMap)
                {
                    list.Add(pair.Value);
                }
            }
        }
        public void GetAllItemById(int itemId,List<PropItem> list)
        {
            if (list != null && createdMap != null)
            {
                foreach (var pair in createdMap)
                {
                    if (pair.Value.ItemId == itemId)
                    {
                        list.Add(pair.Value);
                    }
                }
            }
        }
        public void GetAllItemByType(int type,List<PropItem> list)
        {
            if (list != null && createdMap != null)
            {
                foreach (var pair in createdMap)
                {
                    if (pair.Value.ItemConfig!=null)
                    {
                        if (pair.Value.ItemType == type)
                        {
                            list.Add(pair.Value);
                        }
                    }
                }
            }
        }
        public void GetAllItemByBindId(int bindId,List<PropItem> list)
        {
            if (list != null && createdMap != null)
            {
                foreach (var pair in createdMap)
                {
                    if (pair.Value.BindId == bindId)
                    {
                        list.Add(pair.Value);
                    }
                }
            }
        }
        
    }
}